//
//  WSUpdateFilterConditionCell.m
//  microvision
//
//  Created by zhenjingxiong on 2021/3/5.
//  Copyright © 2021 tencent. All rights reserved.
//

#import "WSUpdateFilterConditionCell.h"
#import "WSDataReportHelper.h"

@interface WSUpdateFilterConditionCell ()

@property (nonatomic, strong) UILabel *keyLabel;


@end

@implementation WSUpdateFilterConditionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.keyLabel];
    self.keyLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addConstraints:[self dropdownListViewConstraints]];
    
    [self.contentView addSubview:self.dropDownButton];
    self.dropDownButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addConstraints:[self dropDownButtonConstraints]];
        
    [self.contentView addSubview:self.valuesTextField];
    self.valuesTextField.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addConstraints:[self valuesTextFieldConstraints]];
}

- (void)setUpdateFilterConditionCentent:(WSDataReportFilterItem *)filterItem {
    self.keyLabel.text = filterItem.reportDataFilterKey;
    self.valuesTextField.text = [filterItem.reportDatafilterValues componentsJoinedByString:@","];
}


- (void)clickDropdownButton:(UIButton *)dropDownbutton {
    if ([self.delegate respondsToSelector:@selector(dropDownbuttonClick:selectIndex:)]) {
        [self.delegate dropDownbuttonClick:dropDownbutton selectIndex:self.index];
    }
}

#pragma mark - Accessor Method

- (UILabel *)keyLabel {
    if (!_keyLabel) {
        _keyLabel = [UILabel new];
        _keyLabel.text = @"position:";
    }
    return _keyLabel;
}

- (UITextField *)valuesTextField {
    if (!_valuesTextField) {
        _valuesTextField = [UITextField new];
        _valuesTextField.placeholder = @"多个值用英文逗号,隔开";
        _valuesTextField.tintColor = [UIColor redColor];
        _valuesTextField.font = [UIFont systemFontOfSize:14];
    }
    return _valuesTextField;
}

- (UIButton *)dropDownButton {
    if (!_dropDownButton) {
        _dropDownButton = [UIButton new];
        _dropDownButton.titleLabel.font = [UIFont systemFontOfSize:10];
        [_dropDownButton setTitle:@"默认值选择" forState:UIControlStateNormal];
        [_dropDownButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_dropDownButton addTarget:self action:@selector(clickDropdownButton:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _dropDownButton;
}

#pragma mark - Automatic layout Method

- (NSArray<NSLayoutConstraint *> *)dropdownListViewConstraints {
    NSLayoutConstraint *separatorLineImageViewCenterY = [WSDataReportHelper equallyRelatedConstraintWithView:self.keyLabel toItem:self.contentView attribute:NSLayoutAttributeCenterY];
    NSLayoutConstraint *separatorLineImageViewLeading = [NSLayoutConstraint constraintWithItem:self.keyLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10];;
    NSArray *constraints = @[separatorLineImageViewLeading, separatorLineImageViewCenterY];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)dropDownButtonConstraints {
    NSLayoutConstraint *dropDownButtonTop = [WSDataReportHelper equallyRelatedConstraintWithView:self.dropDownButton toItem:self.contentView attribute:NSLayoutAttributeTop];
    NSLayoutConstraint *dropDownButtonBottom = [WSDataReportHelper equallyRelatedConstraintWithView:self.dropDownButton toItem:self.contentView attribute:NSLayoutAttributeBottom];
    NSLayoutConstraint *dropDownButtonRight = [WSDataReportHelper equallyRelatedConstraintWithView:self.dropDownButton toItem:self.contentView attribute:NSLayoutAttributeRight];
    NSLayoutConstraint *dropDownButtonWidth = [NSLayoutConstraint constraintWithItem:self.dropDownButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:80];
    NSArray *constraints = @[dropDownButtonTop, dropDownButtonBottom, dropDownButtonRight, dropDownButtonWidth];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)valuesTextFieldConstraints {
    NSLayoutConstraint *valuesTextFieldLeading = [NSLayoutConstraint constraintWithItem:self.valuesTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.keyLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100];
    NSLayoutConstraint *valuesTextFieldRight = [NSLayoutConstraint constraintWithItem:self.valuesTextField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.dropDownButton attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-10];
    NSLayoutConstraint *valuesTextFieldTop = [NSLayoutConstraint constraintWithItem:self.valuesTextField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
    NSLayoutConstraint *valuesTextFieldBottom = [NSLayoutConstraint constraintWithItem:self.valuesTextField attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10];
    NSArray *constraints = @[valuesTextFieldLeading, valuesTextFieldRight, valuesTextFieldTop, valuesTextFieldBottom];
    return constraints;
}


@end
